package com.ruoyi.web.controller.combat.userlocation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.combat.domain.userlocation.ConfuseText;
import com.ruoyi.combat.service.userlocation.IConfuseTextService;
import com.ruoyi.combat.domain.vo.ConfuseTextVo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.file.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 混淆文本库
 * Controller
 *
 * @author ruoyi
 * @date 2025-01-09
 */
@Api(tags = "课题三：网络抗定位-混淆文本库")
@RestController
@RequestMapping("/confuse/text")
public class ConfuseTextController extends BaseController {
    @Autowired
    private IConfuseTextService confuseTextService;

    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询混淆文本库列表
     */
    @ApiOperation(value = "查询混淆文本库列表")
    @PreAuthorize("@ss.hasPermi('system:text:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConfuseText confuseText) {
        startPage();
        List<ConfuseText> list = confuseTextService.selectConfuseTextList(confuseText);
        return getDataTable(list);
    }

    @PostMapping("/getContent")
    @Anonymous
    public AjaxResult getContent(ConfuseText confuseText) {
        List<ConfuseText> confuseTexts = confuseTextService.selectConfuseTextList(confuseText);
        if(confuseTexts.size()>0){
            ConfuseText confuseText1 = confuseTexts.get(0);
            return success((Object) confuseText1.getConfuseContent());
        }
        return success();
    }

    /**
     * 导出混淆文本库列表
     */
    @PreAuthorize("@ss.hasPermi('system:text:export')")
    @Log(title = "混淆文本库 ", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出混淆文本库列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConfuseText confuseText) {
        List<ConfuseText> list = confuseTextService.selectConfuseTextList(confuseText);
        ExcelUtil<ConfuseText> util = new ExcelUtil<ConfuseText>(ConfuseText.class);
        util.exportExcel(response, list, "混淆文本库数据");
    }

    /**
     * 获取混淆文本库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:text:query')")
    @ApiOperation(value = "获取混淆文本库详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(confuseTextService.selectConfuseTextById(id));
    }

    /**
     * 新增混淆文本库
     */
    @PreAuthorize("@ss.hasPermi('system:text:add')")
    @Log(title = "混淆文本库", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增混淆文本库")
    @PostMapping
    public AjaxResult add(@RequestBody ConfuseText confuseText) throws Exception {
//        String folderPath = "E:\\抗映射\\各大城市文本及配图\\配图";
//        File folder = new File(folderPath);
//        if (folder.exists() && folder.isDirectory()) {
//            // 获取文件夹下所有子文件夹
//            File[] subFolders = folder.listFiles(File::isDirectory);
//
//            // 遍历子文件夹
//            for (File subFolder : subFolders) {
//                System.out.println("Found subfolder: " + subFolder.getName());
//                String cityName = subFolder.getName();
//
//                // 读取子文件夹中的文件
//                File[] files = subFolder.listFiles();
//                if (files != null) {
//                    for (File file : files) {
//                        System.out.println("  Reading file: " + file.getName());
//                        String text = file.getName();
//                        if (file.isDirectory()) {
//                            File[] subFolders1 = file.listFiles();
//                            String img = "";
//                            for (File subFolder1 : subFolders1) {
//                                MultipartFile cMultiFile = new MockMultipartFile("file", subFolder1.getName(), "text/plain", new FileInputStream(subFolder1));
//                                String result = sysFileService.uploadFile(cMultiFile);
//                                img = img + "," + result;
//                            }
//                            img = img.substring(1, img.length());
//                            confuseText.setConfuseCity(cityName);
//                            confuseText.setConfuseImgs(img);
//                            confuseText.setConfuseContent(text);
//                            confuseTextService.insertConfuseText(confuseText);
//                        }
//                    }
//                }
//            }
//        } else {
//            System.out.println("The folder does not exist or is not a directory.");
//        }


        return toAjax(confuseTextService.insertConfuseText(confuseText));
    }


    /**
     * 修改混淆文本库
     */
    @PreAuthorize("@ss.hasPermi('system:text:edit')")
    @ApiOperation(value = "修改混淆文本库")
    @Log(title = "混淆文本库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConfuseText confuseText) {
        return toAjax(confuseTextService.updateConfuseText(confuseText));
    }

    /**
     * 删除混淆文本库
     */
    @PreAuthorize("@ss.hasPermi('system:text:remove')")
    @ApiOperation(value = "删除混淆文本库")
    @Log(title = "混淆文本库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(confuseTextService.deleteConfuseTextByIds(ids));
    }

    /**
     *
     * @param confuseTextVo
     * @return
     */
    @Log(title = "混淆文本库", businessType = BusinessType.INSERT)
    @ApiOperation(value = "将算法生成的混淆文本及图片入库")
    @PostMapping("/insertAntiLoc")
    public AjaxResult insertConfuseText(@RequestBody ConfuseTextVo confuseTextVo) throws Exception{
        ConfuseText confuseText = new ConfuseText();
        if (!confuseTextVo.getBase64Imgs().isEmpty()) {
            String img = "";
            for (ConfuseTextVo.Base64Img base64Img : confuseTextVo.getBase64Imgs()) {
                String result = sysFileService.uploadFileBase64(base64Img.getBase64(),
                        base64Img.getContentType(), base64Img.getFileName());
                img = img + "," + result;
            }
            confuseText.setConfuseImgs(img);
        }
        if (!confuseTextVo.getCity().isEmpty())
            confuseText.setConfuseCity(confuseTextVo.getCity());
        if (!confuseTextVo.getText().isEmpty())
            confuseText.setConfuseContent(confuseTextVo.getText());
        return toAjax(confuseTextService.insertConfuseText(confuseText));
    }

}
